//
//  ChangeLoginPasVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/8/3.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ChangeLoginPasVC.h"
#import  "UserService.h"
#import "UIButton+Gradient.h"
#import "UIimage+Extends.h"

@interface ChangeLoginPasVC ()

@property(nonatomic,weak) IBOutlet  UITextField *oldFid;
@property(nonatomic,weak) IBOutlet UITextField *pasnewFid;
@property(nonatomic,weak) IBOutlet UITextField *aginFid;

@property(nonatomic,weak) IBOutlet UIButton *saveBtn;

@property(nonatomic,strong) UserService *userServic;
@end

@implementation ChangeLoginPasVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"修改密码"];
    self.saveBtn.layer.masksToBounds = YES;
    self.saveBtn.layer.cornerRadius = 5;
    [self.oldFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.pasnewFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.aginFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
}

-(void)changedTextField:(UITextField*)fid{
    if(self.pasnewFid.text.length >0 &&
       self.oldFid.text.length >= 6 &&
        self.aginFid.text.length >= 1) {
        self.saveBtn.selected = YES;
        [self.saveBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }else{
        self.saveBtn.selected = NO;
        [self.saveBtn setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.saveBtn.bounds.size] forState:UIControlStateNormal];
    }
}

-(BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}

-(UserService*)userServic{
    if (_userServic == nil) {
        _userServic = [[UserService alloc] init];
    }
    return _userServic;
}

-(IBAction)submitAction:(id)sender{
    
    if (![self checkPassword:self.pasnewFid.text]) {
        
        [WHToast showMessage:@"密码长度6-20位,至少包含字母,数字和标点符号中的两种类型" duration:2 finishHandler:nil];
        return;
    }
    
    TYMWeak(self);
    [self.userServic getUpdatepwd:self.oldFid.text newPassword:self.pasnewFid.text completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
        if (response.message) {
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }else{
            [WHToast showMessage:@"修改成功" duration:2 finishHandler:^{
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}


@end
